﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Runtime.InteropServices;
using WinInterop = System.Windows.Interop;
using System.Windows.Controls.Primitives;
namespace Gronia.WPF.Controls
{
    public partial class CustomWindow : Window
    {
        private void OnSourceInitialized(object sender, EventArgs e)
        {
            _hwndSource = (WinInterop.HwndSource)PresentationSource.FromVisual(this);
            IntPtr handle = (new WinInterop.WindowInteropHelper(this)).Handle;
            WinInterop.HwndSource.FromHwnd(handle).AddHook(new WinInterop.HwndSourceHook(WindowProc));
        }

        private void OnResizeThumbPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Thumb Thumb = sender as Thumb;
            switch (Thumb.Name)
            {
                case "PART_ResizeTop":
                    {
                        ResizeWindow(ResizeDirection.Top);
                    }
                    break;
                case "PART_ResizeBottom":
                    {
                        ResizeWindow(ResizeDirection.Bottom);
                    }
                    break;
                case "PART_ResizeLeft":
                    {
                        ResizeWindow(ResizeDirection.Left);
                    }
                    break;
                case "PART_ResizeRight":
                    {
                        ResizeWindow(ResizeDirection.Right);
                    }
                    break;
                case "PART_ResizeTopLeft":
                    {
                        ResizeWindow(ResizeDirection.TopLeft);
                    }
                    break;
                case "PART_ResizeTopRight":
                    {
                        ResizeWindow(ResizeDirection.TopRight);
                    }
                    break;
                case "PART_ResizeBottomLeft":
                    {
                        ResizeWindow(ResizeDirection.BottomLeft);
                    }
                    break;
                case "PART_ResizeBottomRight":
                    {
                        ResizeWindow(ResizeDirection.BottomRight);
                    }
                    break;
            }
        }

        private void OnDragThumbPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount > 1)
            {
                OnRestoreClick(sender, null);
            }
            else
            {
                if (Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    DoDrag();
                }
            }
        }

        private void DoDrag()
        {
            WIN32Utilities.ReleaseCapture();
            WIN32Utilities.SendMessage(_hwndSource.Handle, 0xA1, (IntPtr)0x2, (IntPtr)0);
        }

        private void OnMinimizeClick(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void OnRestoreClick(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState == WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
        }

        private void OnCloseClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}